package com.poisonh.unifiedauth.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dao.UserinfoDao;
import com.poisonh.unifiedauth.dao.UserinfoDaoImpl;
import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.utils.ActivityStack;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.PreferencesUtils;

import java.util.List;

/**
 * Activity基类
 * Created by PoisonH on 2016/6/23.
 */
public abstract class BaseActivity extends AppCompatActivity
{
    /**
     * 标题栏
     */
    protected LinearLayout mLeftBtn;
    protected LinearLayout mRightBtn;
    protected TextView mTvTitle;
    protected TextView mTvRight;

    /**
     * 用户id
     */
    protected String mStrUserId;
    /**
     * 用户token
     */
    protected String mStrUserToken;
    /**
     * 用户手机
     */
    protected String mStrUserPhone;

    /**
     * 用户头像地址
     */
    protected String mStrUserHeadUrl;
    /**
     * 用户昵称
     */
    protected String mStrUserName;

    /**
     * 用户真实姓名
     */
    protected String mStrUserReallyName;

    /**
     * 用户真实姓名
     */
    protected String mStrUserCard;
    /**
     * 用户邮箱
     */
    protected String mStrUserEmail;

    /**
     * 用户评分
     */
    protected String mStrUserScore;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //在自己的应用初始Activity中加入如下两行代码
//        RefWatcher refWatcher = MyApplication.getRefWatcher(this);
//        refWatcher.watch(this);
        initUserData();
        ActivityStack.getAppManager().addActivity(this);
        setTheme(R.style.AppTheme);
        loadContentView(savedInstanceState);
    }

    /**
     * 初始化顶部标题栏控件
     */
    protected void initTitleView()
    {
        mLeftBtn = (LinearLayout) findViewById(R.id.ib_left_btn);
        mRightBtn = (LinearLayout) findViewById(R.id.ib_right_btn);
        mTvTitle = (TextView) findViewById(R.id.tv_common_title);//中间的tite
        mTvRight = (TextView) findViewById(R.id.tv_right_txt);
    }


    // 加载布局
    protected abstract void loadContentView(Bundle savedInstanceState);

    protected abstract void initView();

    /**
     * activity跳转
     *
     * @param context  上下文
     * @param activity 目标activity
     */
    protected void jumpActivity(Context context, Class activity)
    {
        startActivity(new Intent(context, activity));
    }

    /**
     * 设置居中的文字
     */
    protected void setTitleTxt(String title)
    {
        if (title != null)
        {
            mTvTitle.setText(title);
        }
    }

    protected void setRightBtnBg(int id)
    {
        mRightBtn.setBackgroundResource(id);
    }

    /***
     * 初始化用户数据
     */
    protected void initUserData()
    {
        mStrUserId = PreferencesUtils.getString(this, "ID");

        try
        {

            UserinfoDao mUserinfoDaoImpl = new UserinfoDaoImpl(getApplicationContext());
            List<UserInfoModel> model = mUserinfoDaoImpl.queryReturnList(mStrUserId);
            mStrUserToken = model.get(0).getmStrToken();
            mStrUserCard = model.get(0).getmStrCard();
            mStrUserEmail = model.get(0).getmStrUserEmail();
            mStrUserHeadUrl = model.get(0).getmStrHeadUrl();
            mStrUserName = model.get(0).getmStrUserName();
            mStrUserReallyName = model.get(0).getmStrReallyName();
            mStrUserPhone = model.get(0).getmStrPhone();
            mStrUserScore = model.get(0).getmStringScore();

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        ActivityStack.getAppManager().removeActivity(this);
    }

    @Override
    protected void onRestart()
    {
        super.onRestart();
        long settingTime = PreferencesUtils.getLong(this, "LOCK_SCREEN_TIME", 120);
        long oldTime = PreferencesUtils.getLong(this, "LOCK_TIME");
        long nowTime = System.currentTimeMillis();
        if (settingTime != 10001 && (nowTime - oldTime >= settingTime * 1000))
        {
            ConstantUitls.TYPE = 5;
            jumpActivity(this, SettingPinCodeActivity.class);
        }

    }

    @Override
    protected void onPause()
    {
        super.onPause();
        PreferencesUtils.putLong(this, "LOCK_TIME", System.currentTimeMillis());
    }
}
